package org.jmh.gift.rocketmq.listener;

import cn.hutool.core.bean.BeanUtil;
import org.jmh.gift.constants.OrderConstants;
import org.jmh.gift.dto.PcWebPayDTO;
import org.jmh.gift.dto.PlaceOrderDTO;
import org.jmh.gift.service.IPayOrderService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 下单时创建支付单
 */
@Component
@RocketMQMessageListener(topic = OrderConstants.PLACE_ORDER_TX_TOPIC,
        consumerGroup = OrderConstants.PLACE_ORDER_TX_PAY_CONSUMER_GROUP)
public class PlaceOrderCreatePayOrderMessageListener implements RocketMQListener<MessageExt> {

    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(MessageExt message) {
        String s = new String(message.getBody());
        PlaceOrderDTO dto = JSONObject.parseObject(s, PlaceOrderDTO.class);
        PcWebPayDTO pcWebPayDTO = new PcWebPayDTO();
        BeanUtil.copyProperties(dto,pcWebPayDTO);
        payOrderService.createPayOrder(pcWebPayDTO);
    }
}
